
package com.hoshiicloud.user.controller.company;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.company.BusinessAreasAttachmentType;
import com.hoshiicloud.user.entity.company.CategorySelectionHistory;
import com.hoshiicloud.user.param.company.*;
import com.hoshiicloud.user.service.company.IBusinessAreasAttachmentTypeService;
import com.hoshiicloud.user.service.company.ICategorySelectionHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * <p>
 * 类目选择历史category_selection_history控制器
 * </p>
 *
 * @author campchen
 * @since 2019-08-21
 */

@Slf4j
@RestController
@RequestMapping("/organization/category_selection_history")
@Api(value = "SAAS租户供应商类目选择历史", tags = {"SAAS租户供应商类目选择历史"})
public class CategorySelectionHistoryController {
    @Autowired
    private ICategorySelectionHistoryService categorySelectionHistoryService;


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询类目选择历史category_selection_history
     */
    @ApiOperation(value = "分页查询类目选择历史category_selection_history")
    @PostMapping("/getPage")
    public Response page(@RequestBody CategorySelectionHistoryQueryParam param) {
        try {
            QueryWrapper<CategorySelectionHistory> queryWrapper = new QueryWrapper<CategorySelectionHistory>();
            queryWrapper.lambda()
                    .eq((param.getType() != null && param.getType() != 0), CategorySelectionHistory::getType, param.getType())
                    .eq((param.getCategoryId1() != null && param.getCategoryId1() != 0), CategorySelectionHistory::getCategoryId1, param.getCategoryId1())
                    .eq((param.getCategoryId2() != null && param.getCategoryId2() != 0), CategorySelectionHistory::getCategoryId2, param.getCategoryId2())
                    .eq((param.getCategoryId3() != null && param.getCategoryId3() != 0), CategorySelectionHistory::getCategoryId3, param.getCategoryId3())
                    .eq((param.getDeleted() != null && param.getDeleted() != 0), CategorySelectionHistory::getDeleted, param.getDeleted());
            Page<CategorySelectionHistory> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(categorySelectionHistoryService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询类目选择历史category_selection_history异常", e);
            return Response.failed("分页查询类目选择历史category_selection_history异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询类目选择历史category_selection_history
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询类目选择历史category_selection_history")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(categorySelectionHistoryService.getById(id));
    }


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增类目选择历史category_selection_history")
    public Response save(@RequestBody @Valid CategorySelectionHistoryAddParam param) {
        CategorySelectionHistory data;
        try {
            data = BeanUtils.covertType(param, CategorySelectionHistory.class);
        } catch (Exception e) {
            log.error("新增机构类目选择历史category_selection_history", e);
            return Response.failed("新增类目选择历史category_selection_history Bean转换异常");
        }
        return Response.data(categorySelectionHistoryService.save(data));
    }


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改类目选择历史category_selection_history
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改类目选择历史category_selection_history")
    public Response update(@RequestBody @Valid CategorySelectionHistoryUpdateParam param) {
        CategorySelectionHistory data;
        try {
            data = BeanUtils.covertType(param, CategorySelectionHistory.class);
        } catch (Exception e) {
            log.error("修改类目选择历史category_selection_history Bean转换异常", e);
            return Response.failed("修改类目选择历史category_selection_history Bean转换异常");
        }
        return Response.data(categorySelectionHistoryService.updateById(data));
    }

}

